package com.google.code.sip.ucenter.ws.resources;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.code.sip.ucenter.model.Application;
import com.google.code.sip.ucenter.service.ApplicationManager;
import com.google.code.sip.ucenter.ws.WSResponse;
import com.google.code.sip.ucenter.ws.WSResponseStatus;
import com.google.code.sip.ucenter.ws.dto.ApplicationDto;
import com.sun.jersey.spi.resource.Singleton;

@Component
@Path("/application")
@Singleton
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public class ApplicationResource extends AbstractResource {
	private final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private ApplicationManager applicationManager;

	@POST
	@Path("get")
	public WSResponse<ApplicationDto> getApplication(
			@FormParam("appId") String appId) {
		assert (null != appId) : "appId cann't be null.";
		try {
			Application app = applicationManager.getApplication(appId);
			if (app != null) {
				ApplicationDto dto = toDto(app);
				return new WSResponse<ApplicationDto>(dto);
			} else {
				return new WSResponse<ApplicationDto>(new WSResponseStatus(-1,
						"该应用不存在"));
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<ApplicationDto>(WSResponseStatus.SERVER_ERROR);
		}
	}

	@POST
	@Path("getByName")
	public WSResponse<ApplicationDto> getApplicationByName(
			@FormParam("name") String name) {
		assert (null != name) : "name cann't be null.";
		try {
			Application app = applicationManager.getApplicationByName(name);
			if (app != null) {
				ApplicationDto dto = toDto(app);
				return new WSResponse<ApplicationDto>(dto);
			} else {
				return new WSResponse<ApplicationDto>(new WSResponseStatus(-1,
						"该应用不存在"));
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<ApplicationDto>(WSResponseStatus.SERVER_ERROR);
		}
	}

	@POST
	@Path("list")
	public WSResponse<ApplicationDto> getApplications() {
		try {
			List<ApplicationDto> apps = toDto(applicationManager
					.getAllApplications());
			if (apps != null && !apps.isEmpty()) {
				return new WSResponse<ApplicationDto>(apps);
			} else {
				return new WSResponse<ApplicationDto>();
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<ApplicationDto>(WSResponseStatus.SERVER_ERROR);
		}
	}

	protected List<ApplicationDto> toDto(List<Application> apps) {
		List<ApplicationDto> list = new ArrayList<ApplicationDto>(apps.size());
		if (apps != null && apps.isEmpty()) {
			Iterator<Application> iter = apps.iterator();
			while (iter.hasNext()) {
				ApplicationDto dto = toDto(iter.next());
				list.add(dto);
			}
		}
		return list;
	}

	protected ApplicationDto toDto(Application app) {
		ApplicationDto dto = new ApplicationDto();
		BeanUtils.copyProperties(app, dto);
		return dto;
	}
}
